package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.TreeBuildUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.ZjOrganizationStructureTreeBo;
import com.ruoyi.zhgd.domain.vo.ZjOrganizationStructureTreeVo;
import com.ruoyi.zhgd.domain.ZjOrganizationStructureTree;
import com.ruoyi.zhgd.mapper.ZjOrganizationStructureTreeMapper;
import com.ruoyi.zhgd.service.IZjOrganizationStructureTreeService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 自建部门树结构Service业务层处理
 *
 * @author zyb
 * @date 2023-10-11
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class ZjOrganizationStructureTreeServiceImpl implements IZjOrganizationStructureTreeService {

    private final ZjOrganizationStructureTreeMapper baseMapper;

    /**
     * 查询自建部门树结构
     */
    @Override
    public ZjOrganizationStructureTreeVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询自建部门树结构列表
     */
    @Override
    public TableDataInfo<ZjOrganizationStructureTreeVo> queryPageList(ZjOrganizationStructureTreeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZjOrganizationStructureTree> lqw = buildQueryWrapper(bo);
        Page<ZjOrganizationStructureTreeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询自建部门树结构列表
     */
    @Override
    public List<ZjOrganizationStructureTreeVo> queryList(ZjOrganizationStructureTreeBo bo) {
        LambdaQueryWrapper<ZjOrganizationStructureTree> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZjOrganizationStructureTree> buildQueryWrapper(ZjOrganizationStructureTreeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZjOrganizationStructureTree> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getParentId() != null, ZjOrganizationStructureTree::getParentId, bo.getParentId());
        lqw.eq(bo.getDeptId() != null, ZjOrganizationStructureTree::getDeptId, bo.getDeptId());
        lqw.like(StringUtils.isNotBlank(bo.getDeptName()), ZjOrganizationStructureTree::getDeptName, bo.getDeptName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), ZjOrganizationStructureTree::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增自建部门树结构
     */
    @Override
    public Boolean insertByBo(ZjOrganizationStructureTree bo) {
        ZjOrganizationStructureTree add = BeanUtil.toBean(bo, ZjOrganizationStructureTree.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改自建部门树结构
     */
    @Override
    public Boolean updateByBo(ZjOrganizationStructureTree bo) {
        ZjOrganizationStructureTree update = BeanUtil.toBean(bo, ZjOrganizationStructureTree.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZjOrganizationStructureTree entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除自建部门树结构
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<Tree<Long>> selecttTreeList(ZjOrganizationStructureTree dept) {
        List<ZjOrganizationStructureTree> depts = this.selectStructureList(dept);
        return buildDeptTreeSelect(depts);
    }

    public List<ZjOrganizationStructureTree> selectStructureList(ZjOrganizationStructureTree dept){
        LambdaQueryWrapper<ZjOrganizationStructureTree> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ZjOrganizationStructureTree::getDelFlag,"0")
            .eq(ObjectUtil.isNotNull(dept.getDeptId()),ZjOrganizationStructureTree::getDeptId,dept.getDeptId())
            .eq(ObjectUtil.isNotNull(dept.getParentId()),ZjOrganizationStructureTree::getParentId,dept.getParentId())
            .eq(StringUtils.isNotBlank(dept.getDeptName()), ZjOrganizationStructureTree::getDeptName, dept.getDeptName())
            .eq(StringUtils.isNotBlank(dept.getStatus()), ZjOrganizationStructureTree::getStatus, dept.getStatus())
            .orderByAsc(ZjOrganizationStructureTree::getParentId);
        return baseMapper.selectStructureList(wrapper);
    }


    public List<Tree<Long>> buildDeptTreeSelect(List<ZjOrganizationStructureTree> depts) {
        if (CollUtil.isEmpty(depts)) {
            return CollUtil.newArrayList();
        }
        return TreeBuildUtils.build(depts, (dept, tree) ->
            tree.setId(dept.getDeptId())
                .setParentId(dept.getParentId())
                .setName(dept.getDeptName())
                );
    }

}
